﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Net;
using System.Net.Http;
using System.Web.Http;
using Intelligent.OTC.Common.UnitOfWork;
using Intelligent.OTC.Domain;
using Intelligent.OTC.Business;
using Intelligent.OTC.Domain.Repositories;
using Intelligent.OTC.Domain.DataModel;
using Intelligent.OTC.WebApi.Core;
using Intelligent.OTC.Common;
using Intelligent.OTC.Common.Exceptions;
using Intelligent.OTC.Common.Utils;
using Intelligent.OTC.Common.Attr;

namespace Intelligent.OTC.WebApi.Controllers
{
    [UserAuthorizeFilter(actionSet: "period")]
    public class PeroidController : ApiController
    {
        [HttpGet]
        [PagingQueryable]
        public IQueryable<PeroidReport> Get()
        {
            PeroidService service = SpringFactory.GetObjectImpl<PeroidService>("PeroidService");
            return service.GetAllPeroidReports().AsQueryable();

        }

        [HttpPost]
        public void post([FromBody]List<string> lstEndDate)
        {
            try
            {
                string strEndDate = lstEndDate[0].ToString();
                string strPayment = lstEndDate[1].ToString();
                PeroidService service = SpringFactory.GetObjectImpl<PeroidService>("PeroidService");
                service.newperidAdd(strEndDate, strPayment);
            }
            //catch (OTCServiceException ex)
            //{
            //    throw new OTCServiceException(ex.Message);
            //}
            catch (Exception ex)
            {
                Helper.Log.Error(ex.Message, ex);
                throw new OTCServiceException("DB error!");
            }

        }

        [HttpPost]
        public void post(string type)
        {
            //TODO
            //SOA START
            PeroidService service = SpringFactory.GetObjectImpl<PeroidService>("PeroidService");
            service.StartSOATask();
        }

        [HttpGet]
        public IQueryable<UploadInfo> Get(string Id)
        {
            PeroidService service = SpringFactory.GetObjectImpl<PeroidService>("PeroidService");
            return service.getCurrentPeroidUploadTimes(Convert.ToInt32(Id)).AsQueryable();
        }

        [HttpGet]
        public string get(string type)
        {
            if (type == "Deal")
            {
                //return AppContext.Current.Deal.ToString();
                return AppContext.Current.User.Deal.ToString();
            }
            else
            {
                string[] dts = type.Split(',');

                DateTime dtNow = AppContext.Current.User.Now;
                DateTime dt = Convert.ToDateTime(dts[0] + " 23:59:59");
                DateTime pamentDt = Convert.ToDateTime(dts[1] + " 23:59:59");

                if (dt < dtNow || pamentDt < dtNow)
                {
                    return "2";
                }

                PeroidService service = SpringFactory.GetObjectImpl<PeroidService>("PeroidService");
                var renlist = service.getcurrentPeroid();
                if (renlist == null)
                {
                    return "0";
                }
                else if (renlist.IsCurrentFlg == "0")
                {
                    return "0";
                }
                else
                {
                    return "1";
                }
            }
        }
        [HttpGet]
        public IQueryable<UploadInfo> getInfo(string reportType)
        {
            //Aging
            PeroidService service = SpringFactory.GetObjectImpl<PeroidService>("PeroidService");
            return service.getCurrentPeroidDateSize(reportType).AsQueryable();
        }

        [HttpGet]
        [PagingQueryable]
        public IQueryable<UploadInfoHis> getInfohis(string history)
        {
            FileService service = SpringFactory.GetObjectImpl<FileService>("FileService");
            return service.getFileUploadHis().AsQueryable();
            
        }

        [HttpGet]
        public string getCurrentPer(string cur)
        {
            PeroidService service = SpringFactory.GetObjectImpl<PeroidService>("PeroidService");
            return service.getcurrentPer();

        }

    }
}